package cn.edu.bzu.group4.stockplus.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import cn.edu.bzu.group4.stockplus.po.User;


@SuppressWarnings("unchecked")
@Repository
public class HibernateUserDao extends HibernateDao implements UserDao{
	Session session = null;
	Transaction tran = null;

	@Override
	public User getUser(Long userId) {
		// 根据用户id查询用户
		return (User) getSession().get(User.class, userId);
	}


	@Override
	public User findUser(String username) {
		// 根据用户名查询用户
		Assert.hasText(username);
		String query = "from User u where u.username = :username";
		return (User) getSession().createQuery(query).setString("username", username).uniqueResult();
	}


	@Override
	public void createUser(User user) {
		// 创建用户
		getSession().save(user);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<User> getAllUsers() {
		// 获取所有用户
		return getSession().createQuery("from User order by username").list();
	}


	@Override
	public void deleteUser(Long userId) {
		// 删除用户
		User user = getUser(userId);
		if(user!=null){
			getSession().delete(user);
		}
		
		
	}


	@Override
	public void updateUser(User user) {
		// 更新用户信息
		getSession().update(user);
	}


	@Override
	public User selectUser(String tel, String password) {
		return (User)getSession().createQuery("from User u where u.usertel = ?and u.password= ? ")
				.setParameter(0, tel)
				.setParameter(1, password)
				.uniqueResult();
	}
	
	/*
	 (non-Javadoc)
	 * @see cn.edu.bzu.group4.stockplus.dao.UserDaoo#add(cn.edu.bzu.group4.stockplus.po.User)
	 
	@Override
	public void add(User user){
		session = HibernateSessionFactory.getSession();
		System.out.println("Session: "+session);
		tran = session.beginTransaction();
		System.out.println("Tran: "+tran);
		System.out.println("User:"+user);
		
		session.save(user);
	
		tran.commit();
		if(session != null){
			HibernateSessionFactory.closeSession();
		}
	}
	
	
	 (non-Javadoc)
	 * @see cn.edu.bzu.group4.stockplus.dao.UserDaoo#seleceAllUser()
	 
	@Override
	public List<User> seleceAllUser(){
		
		return null;
	}
	
	//登录
	 (non-Javadoc)
	 * @see cn.edu.bzu.group4.stockplus.dao.UserDaoo#login(java.lang.String, java.lang.String)
	 
	@Override
	public boolean login(String tel,String password){
		
		System.out.println("UserDao login:"+tel+" "+password);
		Session session= openSession();
		String hql = "from User where user_tel=? and user_password=?";
		Query query = session.createQuery(hql);
		query.setParameter(0, tel);
		query.setParameter(1, password);
		List<User> list = query.list();
		
		System.out.println("User List"+list);
		
		if(!list.isEmpty()){
			session.close();
			return true;
		}
		session.close();
		return false;
	}
	
	//注册
	 (non-Javadoc)
	 * @see cn.edu.bzu.group4.stockplus.dao.UserDaoo#regist(cn.edu.bzu.group4.stockplus.po.User)
	 
	@Override
	public boolean regist(User user){
		
		session = HibernateSessionFactory.getSession();
		tran = session.beginTransaction();
		System.out.println("User:"+user);
		session.save(user);
		
		try {
			tran.commit();
			
		} catch (Exception e) {
			System.out.println("用户注册失败，开始回滚...");
			tran.rollback();
		}
		
		if(session != null){
			HibernateSessionFactory.closeSession();
			
			return true;
			
		}
		return false;
	}*/

	
}
